package com.centaline.androidsalesblog.activities.mine;

import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.api.usercenter.UpdateUserInfoApi;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.constants.SprfConstant;
import com.centaline.androidsalesblog.model.databaseobject.UserDo;
import com.centaline.androidsalesblog.model.responseobject.UserRo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centanet.framework.interfaces.IResponseListener;
import com.centanet.framework.utils.SprfUtil;
import com.trello.rxlifecycle.ActivityEvent;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 推送设置
 */
public class PushSettingActivity extends BaseActivity {

    private SwitchCompat sc_push_all, sc_push_im, sc_push_news_post,
            sc_push_news_estate, sc_push_limit;
    private UpdateUserInfoApi updateUserInfoApi;
    private boolean fromUser;
    private boolean userInfoInit;

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()) {
                case R.id.sc_push_all:
                    baiduEvent("N1_2_Type", "推送通知");
                    if (fromUser) {
                        fromUser = false;
                        String pushAll = String.valueOf(isChecked);
                        sc_push_im.setChecked(isChecked);
                        sc_push_news_post.setChecked(isChecked);
                        sc_push_news_estate.setChecked(isChecked);
                        updateUserInfoApi.setIsPushChat(pushAll);
                        updateUserInfoApi.setIsPushEstate(pushAll);
                        updateUserInfoApi.setIsPushPost(pushAll);
                        updateUserInfoApi.setIsPushSystemMsg(pushAll);
                        updateUserInfoApi.startRequest();
                    }
                    break;
                case R.id.sc_push_im:
                case R.id.sc_push_news_post:
                case R.id.sc_push_news_estate:
                    if (buttonView.getId() == R.id.sc_push_im) {
                        baiduEvent("N1_2_Type", "聊天消息");
                    } else if (buttonView.getId() == R.id.sc_push_news_post) {
                        baiduEvent("N1_2_Type", "房源动态");
                    } else if (buttonView.getId() == R.id.sc_push_news_estate) {
                        baiduEvent("N1_2_Type", "小区动态");
                    }
                    if (fromUser) {
                        fromUser = false;
                        updateUserInfoApi.setIsPushSystemMsg(String.valueOf(true));
                        updateUserInfoApi.setIsPushChat(String.valueOf(sc_push_im.isChecked()));
                        updateUserInfoApi.setIsPushPost(String.valueOf(sc_push_news_post.isChecked()));
                        updateUserInfoApi.setIsPushEstate(String.valueOf(sc_push_news_estate.isChecked()));
                        updateUserInfoApi.startRequest();
                    }
                    if (isChecked) {
                        sc_push_all.setChecked(true);
                    }
                    break;
                case R.id.sc_push_limit:
                    SprfUtil.setBoolean(PushSettingActivity.this, SprfConstant.PUSH_LIMIT, isChecked);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.activity_push_setting;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar(R.string.title_push_setting);
    }

    @Override
    protected void initViews() {
        sc_push_all = (SwitchCompat) findViewById(R.id.sc_push_all);
        sc_push_im = (SwitchCompat) findViewById(R.id.sc_push_im);
        sc_push_news_post = (SwitchCompat) findViewById(R.id.sc_push_news_post);
        sc_push_news_estate = (SwitchCompat) findViewById(R.id.sc_push_news_estate);
        sc_push_limit = (SwitchCompat) findViewById(R.id.sc_push_limit);
        sc_push_all.setOnCheckedChangeListener(onCheckedChangeListener);
        sc_push_im.setOnCheckedChangeListener(onCheckedChangeListener);
        sc_push_news_post.setOnCheckedChangeListener(onCheckedChangeListener);
        sc_push_news_estate.setOnCheckedChangeListener(onCheckedChangeListener);
        sc_push_limit.setOnCheckedChangeListener(onCheckedChangeListener);

        updateUserInfoApi = new UpdateUserInfoApi(PushSettingActivity.class, UserRo.class, new IResponseListener<UserRo>() {
            @Override
            public void onResponse(UserRo userRo) {
                UserDo userDo = userRo.getUserDo();
                fromUser = true;
                if (userRo.getResultNo() == 0 &&
                        userDo != null) {
                    boolean pushAll = userDo.isPushChat() || userDo.isPushEstate() ||
                            userDo.isPushPost() || userDo.isPushSystemMsg();
                    sc_push_all.setChecked(pushAll);
                    userSave(userDo);
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                fromUser = true;
                netWorkUnable();
                change();
            }
        });

        change();
    }

    /**
     * 变更
     */
    private void change() {
        RxDataBase.getUser()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<UserDo>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<UserDo>() {
                    @Override
                    public void call(UserDo userDo) {
                        boolean pushAll = userDo.isPushChat() || userDo.isPushEstate() ||
                                userDo.isPushPost() || userDo.isPushSystemMsg();
                        sc_push_all.setChecked(pushAll);
                        sc_push_im.setChecked(userDo.isPushChat());
                        sc_push_news_post.setChecked(userDo.isPushPost());
                        sc_push_news_estate.setChecked(userDo.isPushEstate());
                        sc_push_limit.setChecked(SprfUtil.getBoolean(PushSettingActivity.this, SprfConstant.PUSH_LIMIT, false));
                        fromUser = true;
                        if (!userInfoInit) {
                            userInfoInit = true;
                            updateUserInfoApi.setNickName(userDo.getNickName());
                            updateUserInfoApi.setIsPushSystemMsg(String.valueOf(userDo.isPushSystemMsg()));
                            updateUserInfoApi.setIsPushChat(String.valueOf(userDo.isPushChat()));
                            updateUserInfoApi.setIsPushPost(String.valueOf(userDo.isPushPost()));
                            updateUserInfoApi.setIsPushEstate(String.valueOf(userDo.isPushEstate()));
                        }
                    }
                });
    }

    /**
     * 保存用户信息
     */
    private void userSave(UserDo userDo) {
        AppConstant.setUserLogin(this, userDo);
        RxDataBase.saveUser(userDo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(this.<Boolean>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean aBoolean) {
                    }
                });
    }
}
